package com.hc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.entity.House;
import com.hc.entity.Rent;
import com.hc.entity.Tenant;
import com.hc.service.RentService;
import com.hc.serviceimp.RentServiceImpl;


@WebServlet("/RentServlet")
public class RentServlet extends HttpServlet {

	//创建service层对象
	RentService rService=new RentServiceImpl();
	
    public RentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		
		
		
		if (action.equals("toadd")) {
			List<House> houses = rService.getallhouse();
			List<Tenant> tenants = rService.getallteant();
			
			request.setAttribute("house", houses);
			request.setAttribute("tenant", tenants);
			request.getRequestDispatcher("/WEB-INF/rent/add.jsp").forward(request, response);
			
		}
		if (action.equals("add")) {
			String tid = request.getParameter("tid");
			String hid = request.getParameter("hid");
			String rcount = request.getParameter("rcount");
			String rdate = request.getParameter("rdate");
			String rnote = request.getParameter("rnote");
			
			Rent rent = new  Rent(0, Integer.parseInt(rcount), rdate, rnote, 
					new House(Integer.parseInt(hid), null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Tenant(Integer.parseInt(tid), null, 0, null, null, null, null,0));
			
			rService.addrent(rent);
			request.getRequestDispatcher("RentServlet?action=tolist").forward(request, response);
			
		}
		if (action.equals("tolist")) {
			
			
			request.getRequestDispatcher("/WEB-INF/rent/select.jsp").forward(request, response);
			
		}
		if (action.equals("list")) {
			
			String name = request.getParameter("tname");
			int phone = Integer.parseInt(request.getParameter("tphone"));
			String date = request.getParameter("rdate");
			
			List<Rent> rents = rService.select(name, date, phone);
			request.setAttribute("rent", rents);
			request.getRequestDispatcher("/WEB-INF/rent/list.jsp").forward(request, response);
		}
		
		if (action.equals("toupdate")) {
			String id = request.getParameter("id");
			List<House> getallhouse = rService.getallhouse();
			List<Tenant> getallteant = rService.getallteant();
			Rent rent = rService.show(Integer.parseInt(id));
			
			request.setAttribute("rent", rent);
			request.setAttribute("house", getallhouse);
			request.setAttribute("tenant", getallteant);
			request.getRequestDispatcher("/WEB-INF/rent/edit.jsp").forward(request, response);
		}
		if (action.equals("update")) {
			String rid = request.getParameter("rid");
			String tid = request.getParameter("tid");
			String hid = request.getParameter("hid");
			String rdate = request.getParameter("rdate");
			String rnote = request.getParameter("rnote");
			String rcount = request.getParameter("rcount");
			
			Rent rent = new Rent(Integer.parseInt(rid),Integer.parseInt(rcount) ,rdate, rnote, 
					new House(Integer.parseInt(hid), null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Tenant(Integer.parseInt(tid), null, 0, null, null, null, null,0));
			
			rService.update(rent);
			request.getRequestDispatcher("RentServlet?action=tolist").forward(request, response);
		}
		if (action.equals("del")) {
			String id = request.getParameter("id");
			
			rService.del(Integer.parseInt(id));
			request.getRequestDispatcher("RentServlet?action=tolist").forward(request, response);
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
